<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class commentModel extends Model {

    protected $_time;
    protected $_idPeople;
    protected $_idProduct;
    protected $_content;

    public function setTime($time) {
        $this->_time = $time;
    }

    public function setidPeople($id) {
        $this->_idPeople = $id;
    }

    public function setidProduct($id) {
        $this->_idProduct = $id;
    }

    public function setContent($content) {
        $this->_content = $content;
    }

    public function getTime() {
        return $this->_time;
    }

    public function getidPeople() {
        return $this->_idPeople;
    }

    public function getidProduct() {
        return $this->_idProduct;
    }

    public function getContent() {
        return $this->_content;
    }

    public function storeToDB() {
        $sql = "INSERT INTO comment
			(content, time, idProduct, idPeople)
			VALUES
			(?, ?, ?, ?
			 )";

        $data = array($this->_content, $this->_time, $this->_idProduct, $this->_idPeople);
        $sth = $this->_db->prepare($sql);
        return $sth->execute($data);
    }
    
    public function getCommentByProductId($id) {
        $sql = "SELECT *	
                    FROM comment 
                    WHERE idProduct = '$id'
                    ";

        $this->_setSql($sql);
        $comment = $this->getRow();
        if (empty($comment)) {
            return false;
        }
        else
            return $comment;
    }
    
    public function getNumberOfRowByProductId($id) {
        $sql = "SELECT COUNT(*) as nums
            FROM comment
            WHERE idProduct = '$id'
        ";
        
        $this->_setSql($sql);
        $count = $this->getAll();
        
        if (empty($count)) {
            return 0;
        } else 
            return $count[0]["nums"];
    }
    
    public function getCommentByPage($id, $page) {
        $start = (int)NUMBER_PER_PAGE_COMMENT * $page ;
        $end = (int) NUMBER_PER_PAGE_COMMENT;
        $sql = "SELECT *
            FROM comment
            WHERE idProduct = '$id'
            ORDER BY time DESC
            LIMIT $start,$end
        ";
        
        // echo $sql;
        
        $this->_setSql($sql);
        
        $commentPageReturn = $this->getAll();
        if (empty($commentPageReturn)) {
            return FALSE;
        }
        else 
            return $commentPageReturn;
    }
}

?>
